package com.zbkj.client.task;

import com.zbkj.client.service.impl.VideoServiceCommonImpl;
import com.zbkj.common.model.client.agent.ThirdHot;
import com.zbkj.service.service.ThirdHotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ThirdHotSyncTask {

    @Autowired
    private VideoServiceCommonImpl videoServiceCommonImpl;

    @Autowired
    private ThirdHotService thirdHotService;

    /**
     * 每小时同步抖音热榜
     * cron: 每小时第0分钟执行一次
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void syncDouyinHotList() {
        log.info("ThirdHotSyncTask-开始同步抖音热榜");
        try {
            List<Map<String, String>> douyinResult = videoServiceCommonImpl.getHostList("douyin");
            if (douyinResult == null || douyinResult.isEmpty()) {
                log.info("ThirdHotSyncTask-抖音热榜为空，无需插入");
                return;
            }
            List<ThirdHot> batchList = new ArrayList<>();
            for (Map<String, String> res : douyinResult) {
                ThirdHot item = new ThirdHot();
                item.setTitle(res.get("title"));
                item.setDescription(res.get("content"));
                item.setSource("douyin");
                batchList.add(item);
            }
            boolean saved = thirdHotService.saveBatch(batchList);
            log.info("ThirdHotSyncTask-抖音热榜插入完成，记录数：{}，结果：{}", batchList.size(), saved);
        } catch (Exception e) {
            log.error("ThirdHotSyncTask-同步抖音热榜异常", e);
        }
    }
} 